<?	
	/**
	 * @brief object, array 의 데이터를 보여준다.
	 * @param
	 * @return
	 **/
	function showArray() {
        if(!IS_TEST_MODE) return false;
		$object = func_get_args();
		if ( count( $object ) == 1 ) {
			$object = $object[0];
		}
		
		if ( $object == "-" ) {
			echo str_repeat( "-=", 10 );
		}
		else {
			ob_start(); 
			print_r( $object ); 
			$print = ob_get_clean();
			$print = htmlspecialchars( $print );
		  
			echo sprintf( "<pre>%s</pre>", $print );
		}
	}

	/**
	 * @brief object, array 의 데이터를 보여준다.
	 * @param
	 * @return
	 **/
	function helper($sClassName) {
        $sClassPath = _FRAMEWORK."/helper/".$sClassName."/".$sClassName.".php";
        require_once($sClassPath);

        $sClassName = "helper_".$sClassName;
        return new $sClassName;
    }
	
	/**
	 * @brief object, array 의 데이터를 보여준다.
	 * @param
	 * @return
	 **/
    function debugToFile($service,$return="",$use_template=1, $type="debug",$spend_time=0,$result="F",$output_type='json'){

		$param = "";
        $mem_data = "";
        if($output_type == "array"){
            foreach($_REQUEST as $key =>$val){
                $param .= "REQUEST[$key] :".$val."\n";
            }   
            if(is_array($return)){
                foreach($return as $key =>$val){
                    $val = @strlen($val) > 1000 ? "string(".@strlen($val).")" : $val;
                    if(is_array($val)){
                        foreach($val as $k => $v){
                            $mem_data .= "RETURN[$key][$k] :".$v."\n";
                        }   
                    }   
                    $val = is_array($val) ? "array(".count($val).")" : $val;
                    $mem_data .= "RETURN[$key] :".$val."\n";
                }   
            }else{
                    $mem_data = $return.";\n";
            }   
        }else{

          	ob_start(); 
			print_r( $_REQUEST ); 
			$print = ob_get_clean();
			$param = sprintf( "  %s  ", $print );

          	ob_start(); 
			print_r( $return ); 
			$print = ob_get_clean();
			//$print = htmlspecialchars( $print );
			$mem_data = sprintf( "  %s  ", $print );

			$param_json = displayJson($_REQUEST) ;
			$mem_data_json = displayJson($return); 
        }

        $spend_time = round($spend_time,4);
        //detail display log
        //$str = ($result=="F")? $BG_RED : ""; 
        $str = "==============================================\n";
        $str .= "Service: ".$service."\n";
        $str .= "Page: ".$_SERVER['REQUEST_URI']."\n";
        $str .= "Date   : ".date("Y-m-d (H:i:s)")."\n";
        $str .= "During Time   : ".$spend_time." s\n";
        $str .= "Result   : ".$result." \n";
        $str .= "---*REQUEST*----------------------------------\n";
        $str .= $param;
        $str .= "---*RESPONSE*---------------------------------\n";
        $str .= $mem_data."\n\n";
        $str .= "##############################################\n";
        $str .= "---*REQUEST*----------------------------------\n";
        $str .= $param_json."\n";
        $str .= "---*RESPONSE*---------------------------------\n";
        $str .= $mem_data_json."\n";
        $str .= "==============================================\n";
        //consol($str);

        if($use_template==false) {
            $str = $mem_data.";";
        }   

        $_LOG = $_SERVER['DOCUMENT_ROOT']."/framework/logs";

		//chown($_LOG."/access.log", "www-data");
		//chown($_LOG."/debug.log", "www-data");

        //write short log 
        $log =sprintf("echo '%s:%d:%s:%s' >> %s/access.log",
                      $_SERVER['REQUEST_URI'],
                      time(),
                      $spend_time,
                      $result,
                      $_LOG
                     );  
        $write_log=`$log`;
            
        if($type == "debug") { 
            $detail_log = "\"".$str."\" >> ".$_LOG."/debug.log";
        }elseif($type == "query"){
            $str = str_replace('`','',$str);
            $detail_log = "\"".$str."\" >> ".$_LOG."/".$service.".sql";
        }else{
            $str = str_replace('`','',$str);
            $detail_log = "\"".$str."\" >> ".$_LOG."/".$service.".log";
        }
        $write_log = `echo $detail_log`;
    }   

    function displayJson($mData){
	    $sOutput = str_replace(",",",\n", json_encode( $mData ));
        $sOutput = str_replace("{","{\n",$sOutput);
        $sOutput = str_replace("}","\n}",$sOutput);
        $sOutput = str_replace("[","[\n",$sOutput);
        $sOutput = str_replace("]","\n]",$sOutput);
        $sOutput = str_replace(":","    :   ",$sOutput);

        return $sOutput;
    }

    function v($mData){
        var_dump($mData);
    }

    function hex2bin($str)  {      //-- bin2hex 의 역함수
        $ret_str = "";
        for($i=0;$i<strlen($str);$i+=2)  {
            $ge_str = substr($str,$i,2);
            $ret_str .= chr(hexdec($ge_str));
        }
        
        return $ret_str;
    }

    function create_ini($oData = array(
        "group"     => "",
        "method"    => "",
        "type"      => "",
        "param"     => array(),
        "return"    => array()
    )){
        $iniDir = $_SERVER['DOCUMENT_ROOT']."/application/ini/".$oData['group'];
        $iniPath = $iniDir."/".$oData['method'].".ini";
        if(!is_dir($iniDir)) $mkdir = `mkdir $iniDir`;

        $str = sprintf('[INFORMATION]\n');
        $str .= sprintf("name='%s'\n", $oData['method']);
        $str .= "brief=''\n";
        $str .= sprintf("type='%s'\n", $oData['type']);
        $str .="\n";

        $str .= "[PARAMETER]\n";
        foreach($oData['param'] as $key => $val){
            if(in_array($key, array('_activity', '_group', '_method', '_format', '_type'))) continue;
            $str .= sprintf("%s[name]='%s'\n",$key, $key);
            $str .= sprintf("%s[brief]=''\n",$key);
            $str .= sprintf("%s[type]='%s'\n",$key, gettype($val));
            $str .= sprintf("%s[required]=''\n",$key);
        }
        $str .="\n";

        $str .= "[RETURN]\n";
        foreach($oData['return'] as $key => $val){
            $str .= sprintf("%s[name]='%s'\n",$key, $key);
            $str .= sprintf("%s[brief]=''\n",$key);
            $str .= sprintf("%s[type]='%s'\n",$key, gettype($val));
        }
        $str .="\n";
        if(!file_exists($iniPath)){
            $iniData = "\"".$str."\" > ".$iniPath;
            $s = `echo $iniData`;
        }
    }

    function create_json_file($oData = array(
        "group"     => "",
        "method"    => "",
        "type"      => "",
        "param"     => array(),
        "return"    => array()
    )){

        $sJsonDir = _RESOURCE."/json/".$oData['group'];
        $sJsonFile = $sJsonDir."/".$oData['method'].".json";

        if(file_exists($sJsonFile) || $oData['group'] == "admin") return false;
        $oJsonData = array(
            "INFORMATION"    => array(
                "name"  => $oData['method'],
                "brief" => "",
                "type"  => $oData['type'],
                "is_cache" => 1,
                "cache_time" => 5 
            ),
            "PARAMETER" => recursive($oData['param']),
            "RETURN"    => recursive($oData['return'])
        );

        $result = json_encode($oJsonData);
        $result = str_replace("{","{\n",$result);
        $result = str_replace("}","\n}",$result);
        $result = str_replace("[","[\n",$result);
        $result = str_replace("]","\n]",$result);
        $result = str_replace(",",",\n",$result);
        $w = `touch $sJsonFile`;
        $fp = fopen($sJsonFile,"a");
        $fw = fwrite($fp, $result);
        fclose($fp);
    
    }

    function recursive($oData){

       foreach($oData as $key1 => $val1){
           if(in_array($key1, array('_activity', '_group', '_method', '_format', '_type'))) continue;
            $oReturn[$key1]['name'] = $key1;
            $oReturn[$key1]['brief'] = "";
            $oReturn[$key1]['type'] = gettype($val1);
            $oReturn[$key1]['required'] = 1;
            if(is_array($val1)){
                foreach($val1 as $key2 => $val2){
                    $oReturn[$key1]['child'][$key2]['name'] = $key2;
                    $oReturn[$key1]['child'][$key2]['brief'] = "";
                    $oReturn[$key1]['child'][$key2]['type'] = gettype($key2);
                    $oReturn[$key1]['child'][$key2]['required'] = 1;
                     if(is_array($val2)){
                             foreach($val2 as $key3 => $val3){
                                $oReturn[$key1]['child'][$key2]['child'][$key3]['name'] = $key3;
                                $oReturn[$key1]['child'][$key2]['child'][$key3]['brief'] = "";
                                $oReturn[$key1]['child'][$key2]['child'][$key3]['type'] = gettype($key3);
                                $oReturn[$key1]['child'][$key2]['child'][$key3]['required'] = 1;
                             }
                     }
                        
                }
            }
        }

        return $oReturn;
    }

